<script type="text/javascript">
    function checkComment(){
         temp=$("#editor-comment").val().replace(/ /gi, '');
         if(temp == '' || $("#editor-comment").val()==''||$("#editor-comment").val()=='Mời bạn viết nhận xét'){
             $("#editor-comment").focus();
             return false;
         }
            temp=document.getElementById('name').value.replace(/ /gi, '');
            if(temp==''||document.getElementById('name').value=="Tên của bạn"){
                document.getElementById('mess').innerHTML='Hãy nhập tên';
                document.getElementById('name').innerHTML=temp;
                document.getElementById('name').focus();
                return false;
            }
            if(!check_email('email', 'mess')) return false;
            return true;
        }
        function check_email(email_id,err_id){
            emailRegExp = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.([a-z]){2,4})$/;
            var err_mail='Email không đúng!!';
            if(emailRegExp.test(document.getElementById(email_id).value)) return true;
            else{
                document.getElementById(err_id).innerHTML=err_mail;
                document.getElementById(email_id).focus();
                return false;
            }
        }
        function insert(element, code){
            element = document.getElementById(element);
            if (document.selection){
                element.focus();
                sel = document.selection.createRange();
                sel.text = sel.text;
            }
            else if (element.selectionStart || element.selectionStart == '0'){
                element.focus();
                var startPos = element.selectionStart;
                var endPos = element.selectionEnd;
                element.value = element.value.substring(0, startPos) + code+ element.	value.substring(endPos,
                        element.value.length);
            }
            else{
                element.value += code;
            }
        }
        $(document).ready(function(){
            $("#cm-motion").hide('fast');
            $("#more-smiles").click(function(){
                $("#cm-motion").toggle('slow');
            });
            $("input[name=comment-submit]").click(function(){
                //disable submit button
                $("#comment-submit").attr('disable', 'disable');
                if(!checkComment()) return false;
                $("#cm-content .loading").html(loading);
                $.ajax({
                    type: 'POST',
                    url: host+"<?php echo 'comment/video/'.$movie_id.'/addnew.html'; ?>",
                    data:'submit=1&email='+$("#email").val()+'&name='+$("#name").val()+'&comment='+$("#editor-comment")
                            .val(),
                    success:function(data){
                        var name = $("#name").val();
                        var temp = "<div class=\"cm_info\">"+
                                        "<div class=\"cm_img\"><img src=\""+host+"images/no-ava.png\"/></div>"+
                                        "<div class=\"cm_detail\">"+
                                            "<div class=\"top\">"+
                                                "<span class=\"tcm_tt\"><i>"+name+"</i></span>"+
                                                "<span style=\"color: #555;font-size: 10px;\">1 phút trước</span>"+
                                            "</div>"+
                                            "<div class=\"content_cm\"><div>"+data+"</div></div>"+
                                            "<div class=\"bottom\"><img src=\""+host+"images/bottom.gif\"></div>"+
                                        "</div>"+
                                    "</div>";
                        $("#cm-contents").prepend(temp);
                        $("#email").val('');
                        $("#name").val('');
                        $("#editor-comment").val('');
                        //enable submit button
                        $("#cm-content .loading").html('');
                        $("#mess").html('');
                        $("#comment-submit").attr('disable', '');
                        return false;
                    }
                });
                return false;
            });
        });

</script>
<?php
foreach($comments as $comment):
    $html = parse_smile($searchArray, $replaceArray, $comment['content']);
?>
<div class="cm_info">
    <div class="cm_img"><img src="<?php echo base_url(); ?>images/no-ava.png"/></div>
    <div class="cm_detail">
        <div class="top">
            <span class="tcm_tt"><i><?php echo $comment['name']; ?></i></span>
            <span style="color: #555;font-size: 10px;"><?php echo $comment['time']; ?></span>
        </div>
        <div class="content_cm"><div><?php echo $html; ?></div></div>
        <div class="bottom"><img src="<?php echo base_url();?>images/bottom.gif" alt=""></div>
    </div>
</div>
<?php
endforeach;
?>

<div class="paging">
        <div class="loading"></div>
        <div class="pagination">
        <?php
        //phan trang
        if(count($pageArr) > 0):
            $temp = '';
            //first page
            if($currPage != 1){
                $prePage = $currPage - 1;
                echo "<span onclick=\"viewComment(1)\"> << </span>";
                echo "<span onclick=\"viewComment($prePage)\"> < </span>";
            }
            for($i = 0; $i < count($pageArr); $i++):
                if($pageArr[$i] != $currPage)
                    $temp .= "<span onclick=\"viewComment(".$pageArr[$i].");\">".$pageArr[$i]."</span>";
                else
                    $temp .= "<span class='active'>".$pageArr[$i]."</span> ";

            endfor;
            echo $temp;
            //last page
            if($currPage != $endPage){
                $nextPage = $currPage + 1;
                echo "<span onclick=\"viewComment($nextPage);\"> > </span>";
                echo "<span onclick=\"viewComment($endPage);\"> >> </span>";
            }
        endif;
        ?>

        </div>
    </div>

<form method="post">
    <div id="cm-content">
        <div class="bg-comment" style="float: left;width: 96%;">
            <div class="loading"></div>
            <div class="cm-title">Ý kiến của bạn</div>
            <div class="cm_img"></div>
            <div class="wrapper-n-cm">
                <span>
                    <input type="text" size="30" name="name" value="Tên của bạn" id="name" onfocus="if(this.value=='Tên của bạn') this.value='';" onblur="if(this.value=='') this.value='Tên của bạn';">&nbsp;<span style="color: #000;">(*)</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <input type="text" size="30" name="email" value="Email của bạn" id="email" onfocus="if(this.value=='Email của bạn') this.value='';" onblur="if(this.value=='') this.value='Email của bạn';">&nbsp;<span style="color: #000;">(*)</span>
                </span><br/>
                <span id="mess" class="mess"></span>
                <div class="n-cm-content">
                    <textarea name="editor-comment" id="editor-comment" style="border: none;" rows="5" cols="60" onfocus="if(this.value=='Mời bạn viết nhận xét') this.value='';" onblur="if(this.value=='') this.value='Mời bạn viết nhận xét';"/></textarea>
                    <div align="center" style="width: 100%;float: left;margin: 5px 5px 0 10px;" id="smiley">
                    <?php
                    for($i = 0; $i < 10 && $i < count($smileList); $i++):
                        $img = base_url().$smileList[$i]['path'];
                        $code = $smileList[$i]['text'];
                        echo "<div style='float:left;margin-right: 7px;cursor: pointer;'><img src=\"$img\" alt=\"$code\" onclick=\"insert('editor-comment', '$code')\"></div>";
                    endfor;
                    ?>
                    </div>
                    <div id="cm-motion">
                    <?php
                    for($i = 11; $i < count($smileList); $i++):
                        $img = base_url().$smileList[$i]['path'];
                        $code = $smileList[$i]['text'];
                    ?>
                        <div style='float:left;margin-right: 7px;cursor: pointer;'>
                    <?php
                        echo "<img src=\"$img\" alt=\"$code\" onclick=\"insert('editor-comment', '$code')\">";
                    ?>
                    </div>
                    <?php
                    endfor;
                    ?>
                    </div>
                </div>
                <div style='float: left;'><span id="more-smiles" style="color: #000308; cursor: pointer;
                "><img src="<?php echo base_url(); ?>images/toggle-button.gif"> </span></div>
            </div>
            <div class="submit">
                <input type="button" src="<?php echo base_url(); ?>images/bg-mn-ico.gif" name="comment-submit" id="a"
                       value="Đăng" class="post"/><input type="reset" name="submit" id="b" value="Xóa"/>
            </div>
        </div>
        <div style="float:left;clear: both; width: 96%;">
            <ol>
                <li>- Không "bóc tem"!</li>
                <li>- Không spam, quảng cáo!</li>
                <li>- Từ ngữ lịch sự, thể hiện là người có văn hóa!</li>
            </ol>
        </div>

    </div>
 </form>
